package com.guofen.controller.hsmpy;

import com.guofen.pojo.HsmMiddleWorm;
import com.guofen.pojo.HsmSmallWorm;
import com.guofen.service.hsmpy.MiddleWormBreedingFeedService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;

@RequestMapping("/hsmpy/middleWormBreedingFeed")
@Controller
@Api(tags = "MiddleWormBreedingFeedController", description = "1-3 中虫培育进料记录")
public class MiddleWormBreedingFeedController {

    @Autowired
    private MiddleWormBreedingFeedService middleWormBreedingFeedService;

    @ApiOperation("NAV-返回3-2中还未分盒的n条数据")
    @RequestMapping(value = "/getlist_fh", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist_fh(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getlist_fh());
    }

    @ApiOperation("NAV-返回中虫需要加料的数据")
    @RequestMapping(value = "/getlist", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getlist());
    }

    @ApiOperation("NAV-返回1-3中还未第一次加料的n条数据")
    @RequestMapping(value = "/getlist_first", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist_first(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getlist_first());
    }

    @ApiOperation("NAV-返回1-3中还未第二次加料的n条数据")
    @RequestMapping(value = "/getlist_second", method = RequestMethod.GET)
    @ResponseBody
    public Result getlist_second(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getlist_second());
    }

    @ApiOperation("分盒-添加记录")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name = "id", value = "小虫id", required = false) @RequestParam(required = false) String id,
                      @ApiParam(name = "batch", value = "批次", required = true) @RequestParam String batch,
                      @ApiParam(name = "worm_group", value = "组别", required = true) @RequestParam String worm_group,
                      @ApiParam(name = "small_bug_breed_box_number", value = "微虫培育盒号", required = true) @RequestParam String small_bug_breed_box_number,
                      @ApiParam(name = "big_bug_breed_start_time", value = "中虫培育开始日期", required = true) @RequestParam String big_bug_breed_start_time,
                      @ApiParam(name = "small_bug_box_material_weight", value = "小虫培育盒、虫和渣重量（kg）", required = true) @RequestParam String small_bug_box_material_weight,
                      @ApiParam(name = "small_bug_material_weight", value = "小虫培育虫和渣净重量（kg）", required = true) @RequestParam String small_bug_material_weight,
                      @ApiParam(name = "number", value = "分盒数量", required = true) @RequestParam String number,
                      @ApiParam(name = "per_bug_material_weight", value = "每盒虫及渣（kg/万条虫）", required = true) @RequestParam String per_bug_material_weight,
                      @ApiParam(name = "big_bug_per_box_breed_material_weight", value = "中虫每盒加培育料", required = true) @RequestParam String big_bug_per_box_breed_material_weight,
                      @ApiParam(name = "serial", value = "编号", required = true) @RequestParam String serial,
                      @ApiParam(name = "go_where", value = "大虫去向", required = false) @RequestParam String go_where,
                      @ApiParam(name = "big_bug_breed_box_number", value = "中虫培育盒号", required = false) @RequestParam String big_bug_breed_box_number,
                      @ApiParam(name = "big_bug_number", value = "中虫数量（万条）", required = false) @RequestParam String big_bug_number,
                      @ApiParam(name = "big_bug_material_target_value", value = "中虫中、渣（kg）目标值", required = false) @RequestParam String big_bug_material_target_value,
                      @ApiParam(name = "big_bug_material_get_value", value = "中虫中、渣（kg）获取值", required = false) @RequestParam String big_bug_material_get_value,
                      @ApiParam(name = "big_bug_breed_end_time", value = "大虫出料日期", required = false) @RequestParam String big_bug_breed_end_time){
        Integer Id = null;
        if (id != null){
            Id = Integer.parseInt(id);
        }
        Double smallBugBoxMaterialWeight = Double.parseDouble(small_bug_box_material_weight);
        Double smallBugMaterialWeight = Double.parseDouble(small_bug_material_weight);
        Integer num = Integer.parseInt(number);
        Double perBugMaterialWeight = Double.parseDouble(per_bug_material_weight);
        Double bigBugPerBoxBreedMaterialWeight = Double.parseDouble(big_bug_per_box_breed_material_weight);
        Integer ser = Integer.parseInt(serial);
        Double bigBugNumber = null;
        if (!big_bug_number.isEmpty()){
            bigBugNumber = Double.parseDouble(big_bug_number);
        }
        Double bigBugMaterialTargetValue = null;
        if (!big_bug_number.isEmpty()){
            bigBugMaterialTargetValue = Double.parseDouble(big_bug_material_target_value);
        }
        Double bigBugMaterialGetValue = null;
        if (!big_bug_number.isEmpty()){
            bigBugMaterialGetValue = Double.parseDouble(big_bug_material_get_value);
        }
        if (middleWormBreedingFeedService.add(Id, batch, worm_group, small_bug_breed_box_number, big_bug_breed_start_time, smallBugBoxMaterialWeight, smallBugMaterialWeight, num, perBugMaterialWeight, bigBugPerBoxBreedMaterialWeight,
                ser, go_where, big_bug_breed_box_number, bigBugNumber, bigBugMaterialTargetValue, bigBugMaterialGetValue, big_bug_breed_end_time))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("第一次加料-返回还未第一次加料的n条数据")
    @RequestMapping(value = "/getFirstList", method = RequestMethod.POST)
    @ResponseBody
    public Result getFirstList(@ApiParam(name = "id", value = "小虫培育过程对应的主键ID", required = true) @RequestParam String id){
        Integer Id = Integer.parseInt(id);
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getFirstList(Id));
    }

    @ApiOperation("第一次加料-添加记录")
    @RequestMapping(value = "/first_add", method = RequestMethod.POST)
    @ResponseBody
    public Result first_add(@ApiParam(name = "id", value = "中虫培育过程对应的主键ID", required = true) @RequestParam String id,
                            @ApiParam(name = "big_bug_breed_start_time", value = "中虫培育开始日期", required = true) @RequestParam String big_bug_breed_start_time,
//                            @ApiParam(name = "big_bug_breed_box_weight", value = "中虫培育盒重（g）", required = true) @RequestParam String big_bug_breed_box_weight,
                            @ApiParam(name = "big_bug_breed_box_number", value = "中虫培育盒号", required = true) @RequestParam String big_bug_breed_box_number,
                            @ApiParam(name = "number", value = "数量", required = true) @RequestParam String number,
                            @ApiParam(name = "big_bug_first_breed_material_weight", value = "中虫培育第一次加料重量（kg）", required = true) @RequestParam String big_bug_first_breed_material_weight,
                            @ApiParam(name = "remark", value = "备注", required = false) @RequestParam String remark){
        Integer Id = Integer.parseInt(id);
//        Double big_bug_breed_box_w = Double.parseDouble(big_bug_breed_box_weight);
        Integer num = Integer.parseInt(number);
        Double big_bug_first_breed_material_w = Double.parseDouble(big_bug_first_breed_material_weight);
        if (middleWormBreedingFeedService.first_add(Id, big_bug_breed_start_time, big_bug_breed_box_number, num, big_bug_first_breed_material_w, remark))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("无法继续加料");
    }

    @ApiOperation("第一次加料-返回当天最新一条第一次加料的数据")
    @RequestMapping(value = "/getFirstLatest", method = RequestMethod.POST)
    @ResponseBody
    public Result getFirstLatest(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getFirstLatest());
    }

    @ApiOperation("第二次加料-返回已完成第一次加料、还未第二次加料的n条数据")
    @RequestMapping(value = "/getSecondList", method = RequestMethod.POST)
    @ResponseBody
    public Result getSecondList(@ApiParam(name = "id", value = "小虫培育过程对应的主键ID", required = true) @RequestParam String id){
        Integer Id = Integer.parseInt(id);
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getSecondList(Id));
    }

    @ApiOperation("第二次加料-添加记录")
    @RequestMapping(value = "/second_add", method = RequestMethod.POST)
    @ResponseBody
    public Result second_add(@ApiParam(name = "id", value = "中虫培育过程对应的主键ID", required = true) @RequestParam String id,
                            @ApiParam(name = "second_feed_date", value = "第二次加料日期", required = true) @RequestParam String second_feed_date,
                            @ApiParam(name = "big_bug_second_breed_material_weight", value = "中虫培育第二次加料重量", required = true) @RequestParam String big_bug_second_breed_material_weight,
                            @ApiParam(name = "remark", value = "备注", required = false) @RequestParam String remark) throws ParseException {
        Integer Id = Integer.parseInt(id);
        Double big_bug_second_breed_material_w = Double.parseDouble(big_bug_second_breed_material_weight);
        if (middleWormBreedingFeedService.second_add(Id, second_feed_date, big_bug_second_breed_material_w, remark))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("第二次加料-返回当天最新一条第二次加料的数据")
    @RequestMapping(value = "/getSecondLatest", method = RequestMethod.POST)
    @ResponseBody
    public Result getSecondLatest(){
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getSecondLatest());
    }

    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="batch",value="批次",required=true) @RequestParam String batch,
                         @ApiParam(name="wormGroup",value="组别",required=true) @RequestParam String wormGroup,
                         @ApiParam(name="smallBugBreedBoxNumber",value="孵化盒号",required=true) @RequestParam String smallBugBreedBoxNumber,
                         @ApiParam(name="bigBugBreedBoxNumber",value="中虫培育盒号",required=true) @RequestParam String bigBugBreedBoxNumber,
                         @ApiParam(name="afterBigWeight",value="中虫培育加料重量（kg）",required=true) @RequestParam Double afterBigWeight,
                         @ApiParam(name="number",value="数量",required=true) @RequestParam Integer number,
                         @ApiParam(name="remark",value="备注",required=true) @RequestParam String remark,
                         @ApiParam(name="inspectionRemarks",value="备注",required=true) @RequestParam String inspectionRemarks) {
        HsmMiddleWorm record = new HsmMiddleWorm();
        record.setId(id);
        record.setBatch(batch);
        record.setWormGroup(wormGroup);
        record.setSmallBugBreedBoxNumber(smallBugBreedBoxNumber);
        record.setBigBugBreedBoxNumber(bigBugBreedBoxNumber);
        record.setAfterBigWeight(afterBigWeight);
        record.setNumber(number);
        record.setRemark(remark);
        record.setInspectionRemarks(inspectionRemarks);
        return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(middleWormBreedingFeedService.getByTime(pageSize, currentPage, startTime, endTime));
    }
}
